<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace common\behavior;


use common\model\DirectoryTemplateModel;
use common\model\ProjectTemplateModel;
use support\ErrorCode;
use think\Exception;

/**
 * 处理项目创建之前目录模板的处理
 */
class DealProjectDireTemplateBehavior
{

    /**
     * @param $data
     * @throws Exception
     */
    public function run(&$data)
    {
        $template = $data['project_template_id'] ?? 0;
        if (!empty($template)) {
            $projectTemplate = model(ProjectTemplateModel::class)->field('directory_template_id')->find($template);
            if (!empty($projectTemplate) && empty($projectTemplate['directory_template_id'])) {
                throw new Exception("the project template no choose", ErrorCode::THE_PROJECT_TEMPLATE_NO_CHOOSE);
            }

            if (!empty($projectTemplate['directory_template_id'])) {
                $directoryTemplate = model(DirectoryTemplateModel::class)->field('template_content')->find($projectTemplate['directory_template_id']);
                $data['directory_template'] = $directoryTemplate['template_content'];
            }
        }

    }

}
